package com.rockxin.cat.activity;

import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.adapter.SpinnerAdapter;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.FyBean;
import com.rockxin.cat.bean.MyBankCardBean;
import com.rockxin.cat.bean.RechargeBean;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.JumpActivityManager;
import com.rockxin.cat.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Retrofit;
import rx.Subscriber;

/**
 * 充值
 */
public class RechargeActivity extends BaseActivity {

    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.spinner)
    AppCompatSpinner spinner;
    @BindView(R.id.rechargeCard)
    EditText rechargeCard;
    @BindView(R.id.charge)
    TextView charge;
    @BindView(R.id.next)
    TextView next;
    private SpinnerAdapter adapter;
    private CatNetWork catNetWork;
    private RechargeActivity mRechargeActivity = this;
    private String bankNum;
    private String partner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_recharge;
    }

    @Override
    public void initData() {
        title.setText("充值");

        if (catNetWork == null) {
            catNetWork = new CatNetWork();
        }
        catNetWork.getMyBankList(User.getInstance().getToken(), "list", new Subscriber<HttpResult<List<MyBankCardBean>>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult<List<MyBankCardBean>> myBankCardBeanHttpResult) {
                if (myBankCardBeanHttpResult.getStatus() == 1) {
                    final List<String> strings = new ArrayList<>();
                    final List<MyBankCardBean> data = myBankCardBeanHttpResult.getData();
                    for (int i = 0; i < data.size(); i++) {
                        strings.add(data.get(i).getBank_name());
                    }
                    adapter = new SpinnerAdapter(strings);
                    spinner.setDropDownVerticalOffset(100);//设置选择微调的弹出窗口中像素的垂直偏移。在mode_dropdown唯一有效的
                    spinner.setAdapter(adapter);
                    bankNum = strings.get(0);
                    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            bankNum = data.get(position).getBankcard();
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });

                }
            }
        });


    }

    @OnClick({R.id.back_img, R.id.next, R.id.rechargeCard})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                finish();
                break;
            case R.id.rechargeCard:
                catNetWork.getFyConfig(new Subscriber<FyBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e("-----rechargeCard---->onError");

                    }

                    @Override
                    public void onNext(FyBean fyBean) {
                        if (fyBean.getStatus() == 1) {
                            final FyBean.DataBean data = fyBean.getData();
                            partner = data.getPartner();
                        }
                    }
                });
                break;
            case R.id.next:
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("partner_no", partner);
                hashMap.put("payment", "46");
                hashMap.put("token", User.getInstance().getToken());
                hashMap.put("money", rechargeCard.getText().toString());
                hashMap.put("idno", User.getInstance().getUserInfo().getIdno());
                hashMap.put("real_name", User.getInstance().getUserInfo().getReal_name());
                hashMap.put("bank_card", bankNum);
                catNetWork.recharge(hashMap, new Subscriber<RechargeBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RechargeBean rechargeBean) {
                        if (rechargeBean.getStatus() == 1) {
                            Bundle bundle = new Bundle();
                            bundle.putParcelable("RechargeBean", rechargeBean);
                            JumpActivityManager.getInstance().jumpGoFyPayActivity(mRechargeActivity, bundle);
                        } else {
                            ToastUtils.showLongToast(rechargeBean.getInfo());
                        }
                    }
                });

                break;
        }
    }
}
